package com.demo.page.controller.contact;

import com.demo.common.entity.BranchCompanyDTO;
import com.demo.common.entity.Meta;
import com.demo.page.feign.contact.BranchCompanyFeign;
import com.demo.page.utils.AddressLngLatExchanger;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分公司
 * 作者：张兴然
 * 分配任务：张改
 * 插入数据：贾冠博
 */
@RestController
@CrossOrigin
public class BranchCompanyController {
    @Resource
    private BranchCompanyFeign branchCompanyFeign;

    /**
     * 分页查询所有子公司
     * @param pageNum
     * @return
     */
    @GetMapping("branchCompany")
    public Map selectBranchCompany(@RequestParam(value = "pagenum",defaultValue = "1")Integer pageNum){
        List<BranchCompanyDTO> branchCompanies = branchCompanyFeign.selectBranchCompany(pageNum);

        //将地址转为经纬度
        for (int i = 0; i < branchCompanies.size(); i++) {
            for (int i1 = 0; i1 < branchCompanies.get(i).getAddress().size(); i1++) {
                try {
                    System.out.println("branchCompanies.get(i).getAddress().get(i1).getCompanyAddress() = " + branchCompanies.get(i).getAddress().get(i1).getCompanyAddress());
                    Map point = AddressLngLatExchanger.addressToLngAndLag(branchCompanies.get(i).getAddress().get(i1).getCompanyAddress());
                    branchCompanies.get(i).getAddress().get(i1).setMap(point);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        //全部数据
        List<BranchCompanyDTO> branchCompaniesAll = branchCompanyFeign.selectBranchCompanyAll();
        Map<String,Object> map = new HashMap<>();
        map.put("dates",branchCompanies);
        map.put("totalpage",branchCompaniesAll.size());
        if(branchCompanies.size() > 0){
            map.put("meta",new Meta("200","数据获取成功"));
        }else {
            map.put("meta",new Meta("404","无数据"));
        }
        return map;
    }

    /**
     * 查询子公司下拉模式
     * @return
     */
    @GetMapping("branchCompanyBySize")
    public Map selectBranchCompanyAll(@RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize){
        List<BranchCompanyDTO> branchCompaniesAll = branchCompanyFeign.selectBranchBySize(pageSize);

        //将地址转为经纬度
        for (int i = 0; i < branchCompaniesAll.size(); i++) {
            for (int i1 = 0; i1 < branchCompaniesAll.get(i).getAddress().size(); i1++) {
                try {
                    Map point = AddressLngLatExchanger.addressToLngAndLag(branchCompaniesAll.get(i).getAddress().get(i1).getCompanyAddress());
                    branchCompaniesAll.get(i).getAddress().get(i1).setMap(point);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("dates",branchCompaniesAll);

        if(branchCompaniesAll.size() > 0){
            map.put("meta",new Meta("200","数据获取成功"));
        }else {
            map.put("meta",new Meta("404","无数据"));
        }
        return map;
    }

}
